package DAL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class SqlHelper {
    public static Connection conn;
    private static String driverName = "com.mysql.jdbc.Driver";

    private static String dbURL = "jdbc:mysql://localhost:3306/new";

    private static String userName = "root";

    private static String userPwd = "123456";

    private static Connection getConnection(){
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(dbURL,userName,userPwd);
            return conn;
        }
        catch (Exception e){
            e.printStackTrace();
            System.out.println("----------------连接失败");
        }
        return null;
    }
    public static ResultSet executeQuery(String SQL){
        try {
            Connection conn = getConnection();
            System.out.println("-----------------连接成功");
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);
            return rs;
        }
        catch (Exception e){
            e.printStackTrace();
            System.out.println("-----------------查询失败");
        }
        return null;
    }

    public static boolean executeUpdate(String SQL){
        try {
            Connection conn = getConnection();
            System.out.println("-----------------连接成功");
            Statement stmt = conn.createStatement();
            int result = stmt.executeUpdate(SQL);
            if (result>0)
                return true;
        }
        catch (Exception e){
            e.printStackTrace();
            System.out.println("-----------------更新失败");
        }
        return false;
    }
}
